package com.bigegg.employee.service.impl;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.List;

import com.alibaba.excel.EasyExcel;
import com.bigegg.employee.domain.College;
import com.bigegg.employee.mapper.CollegeMapper;
import com.ruoyi.common.utils.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.bigegg.employee.mapper.MajorMapper;
import com.bigegg.employee.domain.Major;
import com.bigegg.employee.service.IMajorService;
import org.springframework.web.multipart.MultipartFile;

/**
 * 专业Service业务层处理
 * 
 * @author 刘博
 * @date 2022-08-11
 */
@Service
public class MajorServiceImpl implements IMajorService 
{

    public final Logger log = LoggerFactory.getLogger(MajorServiceImpl.class);

    @Autowired
    private MajorMapper majorMapper;

    @Autowired
    private CollegeMapper collegeMapper;

    /**
     * 查询专业
     * 
     * @param id 专业主键
     * @return 专业
     */
    @Override
    public Major selectMajorById(Long id)
    {
        return majorMapper.selectMajorById(id);
    }

    /**
     * 查询专业列表
     * 
     * @param major 专业
     * @return 专业
     */
    @Override
    public List<Major> selectMajorList(Major major)
    {
        return majorMapper.selectMajorList(major);
    }

    /**
     * 新增专业
     * 
     * @param major 专业
     * @return 结果
     */
    @Override
    public int insertMajor(Major major)
    {
        major.setCreateTime(DateUtils.getNowDate());
        return majorMapper.insertMajor(major);
    }

    /**
     * 修改专业
     * 
     * @param major 专业
     * @return 结果
     */
    @Override
    public int updateMajor(Major major)
    {
        return majorMapper.updateMajor(major);
    }

    /**
     * 批量删除专业
     * 
     * @param ids 需要删除的专业主键
     * @return 结果
     */
    @Override
    public int deleteMajorByIds(Long[] ids)
    {
        return majorMapper.deleteMajorByIds(ids);
    }

    /**
     * 删除专业信息
     * 
     * @param id 专业主键
     * @return 结果
     */
    @Override
    public int deleteMajorById(Long id)
    {
        return majorMapper.deleteMajorById(id);
    }

    /**
     * 上传excel
     */
    @Override
    public void importData(MultipartFile multipartFileile){

        InputStream is = null;
        try {
            is = multipartFileile.getInputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }

        List<Major> list = EasyExcel.read(new BufferedInputStream(is)).head(Major.class).sheet().headRowNumber(1).doReadSync();

        if(list!=null && list.size()>0){

            Long cnt = 0L;

            for(Major major : list){

                //
                Major c = new Major();
                BeanUtils.copyProperties(major,c);

                c.setName(major.getName());

                List<Major> majorList = this.majorMapper.selectMajorList(c);

                if(majorList==null || majorList.size()==0){

                    major.setSort(cnt);
                    major.setIsDel(0L);
                    major.setCreateTime(new Date());

                    // 学院id
                    College college = new College();
                    college.setName(major.getCollegeName());
                    List<College> collegeList = this.collegeMapper.selectCollegeList(college);
                    if(collegeList!=null && collegeList.size()>0){
                        College col = collegeList.get(0);
                        major.setCollegeId(col.getId());
                    }

                    this.majorMapper.insertMajor(major);

                }else {

                    this.log.info("此条已存在，无需新增，进行关联");
                }

                cnt++;
                this.log.info(String.format("已完成%d/%d",cnt,list.size()));
            }

        }
    }
}
